package com.traffic.between.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.util.HttpUtil;
import com.traffic.between.util.MD5Util;

public class YiFenXiangServiceImpl {
	private static Logger LOG = Logger.getLogger(YiFenXiangServiceImpl.class);
	public static YiFenXiangServiceImpl service = new YiFenXiangServiceImpl();

	private static String HOST_PATH = BetweenProperties.YIFX_URL;
	private static String YIFX_APPKEY = BetweenProperties.YIFX_APPKEY;
	private static String YIFX_APPSECRET = BetweenProperties.YIFX_APPSECRET;

	public String order(String productName, String mobile, String orderid, String callbackURL) {

		String path = HOST_PATH + "/recharge";

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("appKey", YIFX_APPKEY);
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		params.put("timestamp", timeStamp);

		String signParam = YIFX_APPKEY + timeStamp + YIFX_APPSECRET;
		params.put("secertKey", MD5Util.MD5(signParam).toUpperCase());
		params.put("callbackURL", callbackURL);

		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();
		map.put("productName", productName);
		map.put("mobile", mobile);
		map.put("orderid", orderid);
		list.add(map);
		params.put("orderList", list);

		String reqPara = JSONObject.toJSONString(params);
		LOG.info("**易分享订购参数json字符串: " + reqPara);
		String result = HttpUtil.sendData(path, reqPara);
		LOG.info("yifx order response: " + result);
		return result;
	}

	public String orderStatus(String msgid) {
		LOG.info("yifx orderStatus request: msgid[" + msgid + "]");

		String path = HOST_PATH + "/findRechargeResult";

		Map<String, String> params = new HashMap<String, String>();
		params.put("appKey", YIFX_APPKEY);
		String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
		params.put("timestamp", timeStamp);

		String signParam = YIFX_APPKEY + timeStamp + YIFX_APPSECRET;
		params.put("secertKey", MD5Util.MD5(signParam).toUpperCase());
		params.put("msgid", msgid);

		String reqPara = JSONObject.toJSONString(params);
		String result = HttpUtil.sendData(path, reqPara);
		LOG.info("yifx order status response: " + result);
		return result;
	}
}